package net.ix.opal.data;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.ix.util.IXHttp;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.util.Log;

public class SimpleList{
	private String uri = "http://api.ivanxu.com/a";
	
	private List<Map<String, Object>> list;
	private Map<String, Object> map;
	private IXHttp.HttpsResponse resp;
	private IXHttp.HttpsParams params;
	public SimpleList(){
		params = new IXHttp.HttpsParams();
		params.setParameter("method", "simpleList");
		resp = IXHttp.post(URI.create(uri), params);
		Log.v("RESP", resp.getContent());
	}
	
	public List<Map<String, Object>> List(){
		list = new ArrayList<Map<String, Object>>();
		try{
			StringReader sr = new StringReader(resp.getContent());
			InputSource is = new InputSource(sr);
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(is);
			NodeList nl = doc.getElementsByTagName("list");
			
			for(int i=0;i<nl.getLength();i++){
				map = new HashMap<String, Object>();
				map.put("title",doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue());
				map.put("date",doc.getElementsByTagName("date").item(i).getFirstChild().getNodeValue());
				map.put("key",doc.getElementsByTagName("key").item(i).getFirstChild().getNodeValue());
				list.add(map);
			}
		}catch(Exception e){
		}
		return list;
	}
}
